const path = require('path')
const gulp = require('gulp');
const sequence = require('gulp-sequence') // Run a series of gulp tasks in order.
const order = require("gulp-order");  // 重新排序
const concat = require('gulp-concat')  // 链接文件
const del = require('del');

const uglify = require('gulp-uglify') //minify js
const cleanCss = require('gulp-clean-css') //minify CSS

const sass = require('gulp-sass');
 
gulp.task('css', function () {
  return gulp.src('./src/css/**/*.scss')
    .pipe(sass.sync().on('error', sass.logError))
    .pipe(cleanCss())
    .pipe(gulp.dest('./static/css'));
});

gulp.task('commonJs', function () {
    gulp.src('./src/js/common/**/*.js', { base: './src' })
        .pipe(order([
            'js/common/jquery.js',
            'js/common/*.js'
        ]))
        .pipe(concat('main.min.js'))
        .pipe(uglify({ preserveComments: 'some' }))
        .pipe(gulp.dest('./static/js'))
});

gulp.task('js', function () {
    gulp.src('./src/js/*.js')
        .pipe(uglify({ preserveComments: 'some' }))
        .pipe(gulp.dest('./static/js'))
});

gulp.task('font', function() {
    return gulp.src('./src/fonts/*')
        .pipe(gulp.dest('./static/fonts'))
})

gulp.task('watch', function () {
  gulp.watch('./src/css/**/*.scss', ['css']);
  gulp.watch('./src/js/common/**/*.js', ['commonJs']);
  gulp.watch('./src/js/*.js', ['js']);
});

gulp.task('clean', function () {
    del.sync(['./static'], { force: true })
});

gulp.task('default', ['clean'], function (cb) {
    sequence(['font', 'css', 'commonJs', 'js', 'watch'], cb)
})